package com.infore.siteManagement.util;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.infore.siteManagement.vo.ItemTreeVo;

public class ItemTreeUtil {
	private List<ItemTreeVo> rootList; // 根节点对象存放到这里

	private List<ItemTreeVo> bodyList; // 其他节点存放到这里，可以包含根节点

	public ItemTreeUtil(List<ItemTreeVo> rootList, List<ItemTreeVo> bodyList) {
		this.rootList = rootList;
		this.bodyList = bodyList;
	}

	public List<ItemTreeVo> getTree() { // 调用的方法入口
		if (bodyList != null && !bodyList.isEmpty()) {
			// 声明一个map，用来过滤已操作过的数据
			Map<String, String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
			rootList.forEach(beanTree -> getChild(beanTree, map));
			return rootList;
		} else {
			if (rootList != null && !rootList.isEmpty()) {
				return rootList;
			}
		}
		return null;
	}

	public void getChild(ItemTreeVo beanTree, Map<String, String> map) {
		List<ItemTreeVo> children = Lists.newArrayList();
		bodyList.stream().filter(c -> !map.containsKey(c.getNodeId()))
				.filter(c -> (c.getPid() == null ? "" : c.getPid()).equals(beanTree.getNodeId())).forEach(c -> {
					map.put(c.getNodeId(), c.getPid());
					getChild(c, map);
					children.add(c);
				});
		beanTree.setChildren(children);
	}

}
